package com.corpgovernment.common.validation;

import org.apache.commons.lang3.EnumUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Set;

public class EnumValuesValidator implements ConstraintValidator<EnumValue, String> {
    private Set<String> values;

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        return values.contains(value);
    }

    @Override
    public void initialize(EnumValue enumValue) {
        Class<? extends Enum> value = enumValue.value();
        Set<String> set = EnumUtils.getEnumMap(value).keySet();
        this.values = set;
    }
}
